package com.niiwoo.civet.trade.service.dubbo.statisticsinfo;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.niiwoo.civet.account.enums.AccountAmountTypeEnum;
import com.niiwoo.civet.account.service.MoneyTotalDubboService;
import com.niiwoo.civet.trade.dto.request.thirdplateformdata.DataReportChinaJrbRequestDTO;
import com.niiwoo.civet.trade.dto.request.thirdplateformdata.DataReportJrbRequestDTO;
import com.niiwoo.civet.trade.dto.request.thirdplateformdata.WangDaiTianYanRequestDTO;
import com.niiwoo.civet.trade.dto.request.thirdplateformdata.WangDaiZhiJiaRequestDTO;
import com.niiwoo.civet.trade.dto.response.statisticsinfo.StatisticsInfoResponseDTO;
import com.niiwoo.civet.trade.dto.response.thirdplateformdata.WangDaiTianYanResponseDTO;
import com.niiwoo.civet.trade.dto.response.thirdplateformdata.WangDaiZhiJiaOverDebtResponseDTO;
import com.niiwoo.civet.trade.dto.response.thirdplateformdata.WangDaiZhiJiaProjectResponseDTO;
import com.niiwoo.civet.trade.enums.StatisticsTypeEnum;
import com.niiwoo.civet.trade.report.HttpUploadService;
import com.niiwoo.civet.trade.service.local.statisticsinfo.StatisticsInfoService;
import com.niiwoo.civet.trade.service.local.statisticsinfo.WangdaitianyanLocalService;
import com.niiwoo.civet.trade.service.local.statisticsinfo.WangdaizhijiaLocalService;
import com.niiwoo.civet.trade.service.statisticsinfo.StatisticsInfoDubboService;
import com.niiwoo.civet.user.dto.response.PlatformStatisticsResponseDTO;
import com.niiwoo.civet.user.service.UserBasicInfoDubboService;
import com.niiwoo.tripod.base.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.Date;

/**
 * @Author: wuke
 * @Description: 统计数据相关
 * @Date: Created in 17:35 2017/12/29
 */
@Slf4j
@Service(version = "1.0.0")
public class StatisticsInfoDubboServiceImpl implements StatisticsInfoDubboService {
    @Autowired
    private StatisticsInfoService statisticsInfoService;
    @Reference(version = "1.0.0")
    private MoneyTotalDubboService moneyTotalDubboService;
    @Reference(version = "1.0.0")
    private UserBasicInfoDubboService userBasicInfoDubboService;
    @Autowired
    private WangdaizhijiaLocalService wangdaizhijiaLocalService;
    @Autowired
    private WangdaitianyanLocalService wangdaitianyanLocalService;
    @Autowired
    private HttpUploadService httpUploadService;
//    @Autowired
//    private DataReportForChianHjxhService dataReportForChianHjxhService;

    public StatisticsInfoResponseDTO queryStatisticsInfo(){
        return statisticsInfoService.queryStatisticsInfo();
    }

    //获取总收益
    private BigDecimal queryCumulativeUserProfit(){
        BigDecimal cumulativeUserProfit =  moneyTotalDubboService.queryMoneyTotalByAmountTypeList(
                AccountAmountTypeEnum.INVEST_INCOME,
                AccountAmountTypeEnum.RESEARCH_INCOME,
                AccountAmountTypeEnum.GUARANTEE_INCOME,
                AccountAmountTypeEnum.PLATFORM_GIVEN,
                AccountAmountTypeEnum.PLATFORM_PROVIDE,
                AccountAmountTypeEnum.USER_RED_PACKET
        );
        log.info("StatisticsInfoDubboServiceImpl.queryCumulativeUserProfit.cumulativeUserProfit={}",cumulativeUserProfit);
        return cumulativeUserProfit;
    }

    public void updateStatisticsInfoForJob(){
        //更新累计交易金额
        statisticsInfoService.saveCumulativeTradeAmount();
        //更新申请借款金额
        statisticsInfoService.saveApplyLoanAmount();
        //更新总收益
        statisticsInfoService.saveCumulativeUserProfit(this.queryCumulativeUserProfit());
        //更新注册人数，今日注册人数，借款人数
        this.saveUserRelatedStatistics();
    }

    /**
     *保存注册人数
     *今日注册人数
     *借款人数
     */
    private void saveUserRelatedStatistics(){
        try {
            PlatformStatisticsResponseDTO responseDTO = userBasicInfoDubboService.platformStatisticsInformation();
            log.info("saveUserRelatedStatistics.responseDTO={}", JSON.toJSONString(responseDTO));

            int saveCount = statisticsInfoService.saveStatisticsInfo(StatisticsTypeEnum.REGISTER_USER_COUNT,BigDecimal.valueOf(responseDTO.getRegisterUserCount()));
            log.info("saveUserRelatedStatistics.RegisterUserCount={},saveCount={}",responseDTO.getRegisterUserCount(),saveCount);

            saveCount += statisticsInfoService.saveStatisticsInfo(StatisticsTypeEnum.TODAY_REGISTER_USER_COUNT,BigDecimal.valueOf(responseDTO.getTodayRegisterUserCount()));
            log.info("saveUserRelatedStatistics.TodayRegisterUserCount={},saveCount={}",responseDTO.getTodayRegisterUserCount(),saveCount);

            saveCount += statisticsInfoService.saveStatisticsInfo(StatisticsTypeEnum.APPLY_LOAN_USER_COUNT,BigDecimal.valueOf(responseDTO.getApplyLoanUserCount()));
            log.info("savaUserRelatedStatistics.ApplyLoanUserCount={},saveCount={}",responseDTO.getApplyLoanUserCount(),saveCount);

        } catch (Exception e) {
            log.error("savaUserRelatedStatistics error",e);
        }
    }

    public WangDaiZhiJiaProjectResponseDTO getProjectsByDateForWdzj(WangDaiZhiJiaRequestDTO dto){
        Assert.notNull(dto,"参数非法");
        Assert.notNull(dto.getDate(),"参数非法");
        Assert.notNull(dto.getCurrentPage(),"参数非法");
        return wangdaizhijiaLocalService.getProjectsByDate(dto);
    }

    @Override
    public WangDaiZhiJiaOverDebtResponseDTO getOverdebtProjectsWdzj(WangDaiZhiJiaRequestDTO dto) {
        Assert.notNull(dto,"参数非法");
        Assert.notNull(dto.getDate(),"参数非法");
        Assert.notNull(dto.getCurrentPage(),"参数非法");
        return wangdaizhijiaLocalService.getOverdebtProjects(dto);
    }
    @Override
    public WangDaiTianYanResponseDTO getProjectsByDateForTianyan(WangDaiTianYanRequestDTO dto){
        Assert.notNull(dto,"参数非法");
        Assert.notNull(dto.getPageIndex(),"参数非法");
        Assert.notNull(dto.getPageSize(),"参数非法");
        Assert.notNull(dto.getStatus(),"参数非法");
        Assert.notNull(dto.getTimeFrom(),"参数非法");
        Assert.notNull(dto.getTimeTo(),"参数非法");
        return wangdaitianyanLocalService.getProjectsByDate(dto);
    };

    @Override
    public WangDaiTianYanResponseDTO getInvestorOrdersForTianyan(WangDaiTianYanRequestDTO dto){
        Assert.notNull(dto,"参数非法");
        Assert.notNull(dto.getPageIndex(),"参数非法");
        Assert.notNull(dto.getPageSize(),"参数非法");
        Assert.notNull(dto.getStatus(),"参数非法");
        Assert.notNull(dto.getTimeFrom(),"参数非法");
        Assert.notNull(dto.getTimeTo(),"参数非法");
        return wangdaitianyanLocalService.getInvestorOrders(dto);
    }

    @Override
    public WangDaiTianYanResponseDTO getOverdebtProjectsForTianyan(WangDaiTianYanRequestDTO dto){
        Assert.notNull(dto,"参数非法");
        Assert.notNull(dto.getPageIndex(),"参数非法");
        Assert.notNull(dto.getPageSize(),"参数非法");
        Assert.notNull(dto.getStatus(),"参数非法");
        Assert.notNull(dto.getTimeFrom(),"参数非法");
        Assert.notNull(dto.getTimeTo(),"参数非法");
        return wangdaitianyanLocalService.getOverdebtProjects(dto);
    }
    /**
     * 金融办数据上报
     */
    @Override
    public int reportDataToJrb(DataReportJrbRequestDTO dto){
        log.info("金融办数据上报入参{}",JSON.toJSONString(dto));
        httpUploadService.report(dto.getReportCodes(),dto.getReportDate(),dto.getIsAutoUpload());
        log.info("金融办数据上报入参end");
        return 1;
    }

    @Override
    public int reportDataToChinaJrb(DataReportChinaJrbRequestDTO dto) throws IOException {
        Date allReportCompareDate = null;
        try {
            allReportCompareDate = DateUtils.parseDateFromString(dto.getAllReportCompareDate(), DateUtils.DATE_FORMAT);
        }catch (Exception e){
            log.error("日期比较日期有误{}",JSON.toJSONString(dto),e);
            throw e;
        }
        //一次上模拟数据,仅用于上线之前用于验证数据入库率
        if (dto.getReportDate().equalsIgnoreCase("20181101_test")){
            //dataReportForChianHjxhService.handTestData();
        }else {
            //日报上报
            //dataReportForChianHjxhService.dataReportDate(dto.getReportDate(), dto.getIsAutoUpload(), dto.getIsMonthReport(), allReportCompareDate);
        }
        return 1;
    }

    @Override
    public String queryReportDataToChinaJrb(String fileNme) throws IOException {
        //return dataReportForChianHjxhService.queryTaskStatus(fileNme);
        return "";
    }
}
